package com.hongyuan.venue.ui.home.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import com.google.android.material.tabs.TabLayoutMediator;
import com.hongyuan.venue.R;
import com.hongyuan.venue.base.AppViewModelFactory;
import com.hongyuan.venue.base.CustomFragment;
import com.hongyuan.venue.data.Constants;
import com.hongyuan.venue.databinding.FragmentHomeBinding;
import com.hongyuan.venue.myview.StickyScrollView;
import com.hongyuan.venue.ui.Page2Adapter;
import com.hongyuan.venue.ui.home.beans.Venue;
import com.hongyuan.venue.ui.home.view_mode.FragmentHomeViewModel;
import com.hongyuan.venue.utils.GsonUtil;
import com.hongyuan.venue.utils.StatusBarUtil;
import java.util.ArrayList;
import java.util.List;
import me.tatarka.bindingcollectionadapter2.BR;

public class HomeFragment extends CustomFragment<FragmentHomeBinding, FragmentHomeViewModel> implements StickyScrollView.ScrollViewListener{

    private List<CustomFragment> fragments;
    //渐变高度
    private int height;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_home;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public FragmentHomeViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(FragmentHomeViewModel.class);
    }

    @Override
    public void initView() {
        super.initView();
        setOnRefresh(binding.refresh,REFRESH_0X4);

        binding.line.setBackgroundColor(Color.argb((int) 0, 238,238,238));
        binding.myTitle.setTextColor(Color.argb((int) 0, 51,51,51));
        StatusBarUtil.setCommonUI(getActivity(),false);
        initListeners();
    }

    @Override
    public void initData() {

        //获取支持门店列表
        viewModel.clearParams();
        viewModel.requestData(Constants.GET_MEMEBER_OS);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.showScheduled.observe(this, gcListBeans -> {
            if(gcListBeans != null && gcListBeans.size() > 0){
                fragments = new ArrayList<>();
                for(Venue.GcListBean gcListBean : gcListBeans){
                    String datas = GsonUtil.getGson().toJson(gcListBean.getType());
                    Bundle bundle = new Bundle();
                    bundle.putString("dates",datas);
                    bundle.putString("gc_id",String.valueOf(gcListBean.getGc_id()));

                    fragments.add(new HomeScheduledFragment().setMyArguments(bundle)
                            .setTabTag(gcListBean.getGc_name()));
                }

                binding.viewPager.setAdapter(new Page2Adapter(getActivity(),fragments));
                TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(binding.layoutMenu, binding.viewPager, (tab, position) -> {
                    CustomFragment f = fragments.get(position);
                    tab.setText(f.getTabTag());
                });
                tabLayoutMediator.attach();
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        binding.banner.pause();//暂停轮播
    }

    @Override
    public void onResume() {
        super.onResume();
        binding.banner.start();//开始轮播
    }

    /*
     * 获取头部图片的高度
     * */
    private void initListeners() {
        ViewTreeObserver vto = binding.topBg.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                binding.titleBox.getViewTreeObserver().removeGlobalOnLayoutListener(
                        this);
                height = binding.topBg.getHeight();

                binding.scroll.setScrollViewListener(HomeFragment.this);
            }
        });
    }

    @Override
    public void onScrollChanged(StickyScrollView scrollView, int x, int y, int oldx, int oldy) {
        if (y <= 0) {   //设置标题的背景颜色
            binding.titleBox.setBackgroundColor(Color.argb((int) 0, 255,255,255));
            binding.line.setBackgroundColor(Color.argb((int) 0, 238,238,238));
            binding.myTitle.setTextColor(Color.argb((int) 0, 51,51,51));
        } else if (y > 0 && y <= height) {
            float scale = (float) y / height;
            float alpha = (255 * scale);

            binding.titleBox.setBackgroundColor(Color.argb((int) alpha, 255,255,255));
            binding.line.setBackgroundColor(Color.argb((int) alpha, 238,238,238));
            binding.myTitle.setTextColor(Color.argb((int) alpha, 51,51,51));
            if(y <= height/2){
                StatusBarUtil.setCommonUI(getActivity(),false);
            }else{
                StatusBarUtil.setCommonUI(getActivity(),true);
            }
        } else {
            binding.myTitle.setTextColor(Color.argb( 255, 51,51,51));
            binding.titleBox.setBackgroundColor(Color.argb((int) 255, 255,255,255));
            binding.line.setBackgroundColor(Color.argb((int) 255, 238,238,238));
            StatusBarUtil.setCommonUI(getActivity(),true);
        }
    }
}
